LLDB

About
  • C, C++, Objective-C, Swift

  • Can read

    • PDB (Microsoft debug format)

    • DWARF

  • Platforms

    • Linux:

      • Full support

    • macOS:

      • Full support (default debugger for Apple)

    • Windows:

      • Partial support (via LLVM project; not as mature)

  • Pros

    • Modern design; better macOS support; faster in some tasks

  • Cons

    • Less mature than GDB in some Linux environments.

Installation
  • With MSYS2:

    • MINGW64: pacman -S mingw-w64-x86_64-lldb

      • At C:/msys64/mingw64/bin .

    • UCRT64: pacman -S mingw-w64-ucrt-x86_64-lldb

      • At C:/msys64/ucrt64/bin .

    • CLANG64: pacman -S mingw-w64-clang-x86_64-lldb

      • At C:/msys64/clang64/bin .

      • I opted for this one.

Config

  • File .lldbinit  to save breakpoints/stop points, etc.

    • Has to be saved in the home directory.

My config
  • At C:\Users\caior\.lldbinit

settings set stop-line-count-before 10

settings set stop-line-count-after 10

target stop-hook add -o "shell echo -------------------------------------------------------------------------------------------" -o "bt"

Commands

  • LLDB Command List

  • Open:

    • lldb ./file.exe

  • Open with TUI:

    • lldb --tui ./file.exe

    • Or while inside lldb: gui .

      • "error: lldb was not built with gui support"

      • On MSYS2 (clang64), the prebuilt lldb package is compiled without curses, so gui/--tui won’t work out of the box. You need to build LLDB yourself with curses enabled.

  • Process:

    • process launch -v DEBUG=1

      • Set environment variables for process and launch process in one command

    • process attach --pid 123  / attach -p 123

      • Attach to process.

    • process attach --name a.out  / pro at -n a.out

      • Attach to a process named a.out

    • process attach --name a.out --waitfor

      • Wait for a process named a.out to launch and attach

  • Repeat last command:

    • <nothing>

    • Just press enter without typing anything and the last command will be send again.

  • Quit:

    • quit

  • Help:

    • help

  • Run:

    • run  / run arg1 arg2

  • Attach to a running process:

    • lldb -p <pid>

  • Stop:

    • process interrupt  or ctrl + c

  • Breakpoint:

    • Add:

      • b main  / breakpoint set --name main

      • b main.c:10  / breakpoint set --file main.c --line 10

    • List:

      • br list

    • Delete:

      • br del 4

        • Deletes the 4th breakpoint.

    • Delete all:

      • br del

  • Continue:

    • continue  / c

  • Watchpoints (track variable changes):

    • watchpoint set variable x  / wa s v x

      • Breaks when x  changes.

    • watchpoint list  / watch l

      • List all watchpoints

    • watchpoint delete 1  / watch del 1

      • Delete a watchpoint

  • Step over:

    • next  / n  / thread step-over

  • Step in:

    • step  / s  / thread step-in

  • Step out ("run until the current function returns, then pause in the caller"):

    • finish

  • Step over a single instruction:

    • ni  / thread step-inst-over

  • Step in a single instruction:

    • si  / thread step-inst

  • Show a specific variable:

    • print x   p x

      • Prints x .

    • p *x

      • Prints the deref of x

    • p x->field

      • Prints the field inside the deref of x

    • p x[0]

    • p x[0..5]

  • fr v  / frame variable

    • Show the arguments and local variables for the current frame

    • fr v -f x bar  / frame variable --format x bar

      • Show the contents of local variable bar formatted as hex

  • fr v -a  / frame variable --no-args

    • Show the local variables for the current frame

  • ta v x  / target variable baz

    • Show the contents of global variable baz

  • Frame:

    • frame info

      • Show information about the current frame.

  • Refresh the view:

    • fr s  / frame select

  • Backtrace (callstack):

    • bt  / thread backtrace

  • Memory:

    • memory read <address>

      • Read memory at a specific address.

    • memory write <address> <value>

      • Write a value to a specific memory address.

  • Disassemble:

    • disassemble

      • Show the disassembly of the current function.

  • Threading:

    • thread list

      • List all threads in the program.

    • thread select <id>

      • Switch to a specific thread by ID.

    • frame select <id>

      • Switch to a specific frame in the call stack.

    • thread return <RETURN EXPRESSION>

      • Return immediately from the currently selected frame, with an optional return value

    • bt all  / thread backtrace all

      • Show the stack backtraces for all threads

  • Environment Variables:

    • settings set target.env-vars <key>=<value>

      • Set an environment variable for the target program.

    • settings show target.env-vars

      • Show all environment variables set for the target.

  • Show source code:

    • list

  • Show threads:

    • thread list

  • Stop Hooks:

    • a list of commands that LLDB automatically runs every time the program stops.

    • β€œStops” includes:

      • after next / step

      • when hitting a breakpoint

      • when a signal/exception occurs

    • Ex:

      • Backtrace and disassemble every time you stop

      (lldb) target stop-hook add
      Enter your stop hook command(s). Type 'DONE' to end.
      > bt
      > disassemble --pc
      > DONE
      Stop hook #1 added.
      
    • target stop-hook list

    • target stop-hook delete 1

    • target stop-hook delete

      • Delete all.

    • target stop-hook add -f main.cpp -o "bt"

      • Only for a specific file

    • target stop-hook add -n my_function -o "bt"

      • Only for a function

    • target stop-hook add -s my_binary -o "bt"

      • Only for a module (binary/library)

  • Expression:

    • expression <expr>

      • Evaluate and execute a new expression.